package com.py.custom.common.web.config;

import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import com.py.custom.common.web.exception.GlobalExceptionHandler;
import com.py.custom.common.web.utils.ApplicationContextUtils;
import com.py.custom.common.web.utils.RestTemplateUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
 * description：自动装配类
 *
 * @author pengyou
 * @version 1.0.0
 * @date 2021/10/16
 */
@Configuration
@ComponentScan("com.py.custom.business")
@EnableApolloConfig
public class WebAutoConfiguration {

    @Bean
    public GlobalExceptionHandler globalExceptionHandler() {
        return new GlobalExceptionHandler();
    }

    @Bean
    public ApplicationContextUtils applicationContextUtils() {
        return new ApplicationContextUtils();
    }

    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
        return new RestTemplate(factory);
    }

    @Bean
    public RestTemplateUtils restTemplateUtils( ) {
        return new RestTemplateUtils();
    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        //TODO 后期参数走配置文件
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(5000);//ms
        factory.setConnectTimeout(15000);//ms
        return factory;
    }
}
